$ontext
[1]        $model:basic
[2]        
[3]        $sectors:
[4]        	y(s)	! Production sector
[5]        	c	! Consumption activity
[6]        
[7]        $commodities:
[8]        	p(s)	! Output prices
[9]        	pf(f)	! Facgtor prices
[10]       	pc	! Consumer price index
[11]       
[12]       $consumers:
[13]       	ra	! Representative agent
[14]       
[15]       $prod:y(s)          s:1.0
[16]         o:p(s)            q:supply(s)
[17]         i:pf(f)           q:factor(f,s)
[18]       
[19]       $prod:c             s:1.0
[20]         o:pc              q:cons
[21]         i:p(o)            q:demand(o)
[22]       
[23]       $demand:ra
[24]         d:pc
[25]         e:pf(f)           q:endow(f)
$offtext

*   Alert the user to undefined sets or parameters which are referenced in the MPSGE model:

$if not settype s          $abort "s should be a SET."
$if not settype f          $abort "f should be a SET."
$if not settype o          $abort "o should be a SET."
$if not partype supply     $abort "supply should be a PARAMETER."
$if not dimension 1 supply $abort "supply has an inconsistent dimension."
$if not partype factor     $abort "factor should be a PARAMETER."
$if not dimension 2 factor $abort "factor has an inconsistent dimension."
$if not partype cons       $abort "cons should be a PARAMETER."
$if not dimension 0 cons   $abort "cons has an inconsistent dimension."
$if not partype demand     $abort "demand should be a PARAMETER."
$if not dimension 1 demand $abort "demand has an inconsistent dimension."
$if not partype endow      $abort "endow should be a PARAMETER."
$if not dimension 1 endow  $abort "endow has an inconsistent dimension."

*    Define aliases for sets:

alias (s,s_); 
alias (f,f_); 
alias (o,o_); 

*    Benchmark costs and value shares:

PARAMETER

*    Benchmark costs:

        c0_002(s,f) Benchmark cost for input pf(f) in sector y(s)
        c0_001(s)   Benchmark cost for input nest s in sector y(s)
        c0_006(o)   Benchmark cost for input p(o) in sector c
        c0_005      Benchmark cost for input nest s in sector c

*    Benchmark revenue:

        r0_004(s) Benchmark revenue for output p(s) in sector y(s)
        r0_003(s) Benchmark revenue for output nest t in sector y(s)
        r0_008    Benchmark revenue for output pc in sector c
        r0_007    Benchmark revenue for output nest t in sector c

*    Benchmark shares:

        theta_002(s,f) Benchmark share for input  pf(f) in nest s for sector y(s)
        theta_004(s)   Benchmark share for output p(s) in nest t for sector y(s)
        theta_006(o)   Benchmark share for input  p(o) in nest s for sector c
        theta_008      Benchmark share for output pc in nest t for sector c
;



*   Invoke the function preprocessor


$sysinclude gams-f

c0_002(s,f) = factor(f,s);      
r0_004(s) = supply(s);          
c0_001(s) =+sum(f,c0_002(s,f)); 
r0_003(s) =+r0_004(s);          
c0_006(o) = demand(o);          
r0_008 = cons;                  
c0_005 =+sum(o,c0_006(o));      
r0_007 =+r0_008;                
theta_002(s,f) = c0_002(s,f)/c0_001(s); 
theta_004(s) = r0_004(s)/r0_003(s);     
theta_006(o) = c0_006(o)/c0_005;        
theta_008 = r0_008/r0_007;              

*   Cost functions

p_002(s,f) == pf(f);                            
p_004(s) == p(s);                               
p_001(s) == prod(f,p_002(s,f)**theta_002(s,f)); 
p_003(s) == theta_004(s)*p_004(s);              
p_006(o) == p(o);                               
p_008 == pc;                                    
p_005 == prod(o,p_006(o)**theta_006(o));        
p_007 == theta_008*p_008;                       

*   Quantity functions

q_002(s,f) == factor(f,s); 
q_004(s) == supply(s);     
q_006(o) == demand(o);     
q_008 == cons;             

*   Compensated demand indices:

d_001(s) ==  1;                                     
d_002(s,f) == d_001(s)* (p_001(s)/p_002(s,f))**1.0; 
d_005 ==  1;                                        
d_006(o) == d_005* (p_005/p_006(o))**1.0;           

*   Compensated supply indices:

s_003(s) ==  1;                               
s_004(s) == s_003(s)* (p_003(s)/p_004(s))**0; 
s_007 ==  1;                                  
s_008 == s_007* (p_007/p_008)**0;             

*   Endowments:

e_009(f) == endow(f); 


POSITIVE VARIABLES
        	y(s)	  Production sector
        	c	     Consumption activity
        	p(s)	  Output prices
        	pf(f)	 Facgtor prices
        	pc	    Consumer price index
        	ra	    Representative agent


EQUATIONS
        prf_y(s)  "Production sector"
        prf_c     "Consumption activity"
        mkt_p(s)  "Output prices"
        mkt_pf(f) "Facgtor prices"
        mkt_pc    "Consumer price index"
        inc_ra    "Representative agent"
;





MODEL basic / prf_y.y,prf_c.c,mkt_p.p,mkt_pf.pf,mkt_pc.pc,inc_ra.ra/; 

*     Level value assignments
y.l(s) = 1;  
c.l = 1;     
p.l(s) = 1;  
pf.l(f) = 1; 
pc.l = 1;    

*     Zero profit conditions
prf_y(s).. c0_001(s)*p_001(s) =G= r0_003(s)*p_003(s);
prf_c..    c0_005*p_005 =G= r0_007*p_007;

*     Market clearance conditions
mkt_p(s)..  
            s_004(s)*y(s)*q_004(s)+sum((o)$(sameas(s,o)),-d_006(o)*c*q_006(o)) =e= 0;
mkt_pf(f).. 
            +sum(s,-d_002(s,f)*y(s)*q_002(s,f)) + e_009(f) =e= 0;
mkt_pc..    
            s_008*c*q_008-ra/pc =e= 0;
